<?php
/**
 * 会员卡订单
 * User: ws
 * Date: 2018/8/31
 * Time: 16:46
 */

namespace app\admin\controller;
use app\admin\model\CardOrder;
use think\Db;
class Corder  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';

            if(is_numeric(input('type')) === true){
                $where['m.type'] = input('type');
            }
            $where['c.card_order_sn|c.exempted_quota|u.name|u.phone'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_card_order')->alias('c')->join('je_membership_card m','c.card_id = m.card_id')->join('je_user u','c.user_id = u.user_id')->field('u.name as uname,u.phone,m.type as stype,c.*')->order('card_order_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$item){
                if($item['channel']==1){
                    $list['data'][$key]['stores'] = Db::table('je_store_user')->alias('su')->join('je_store s','su.store_id = s.store_id')->field('su.name as suname,s.name as sname') ->where("su.sn='{$item['store_user_sn']}'")->find();
                }elseif ($item['channel']==3){
                    $list['data'][$key]['stores'] = db('user')->where('user_id',$item['store_user_sn'])->value('name');
                } else{
                    $list['data'][$key]['stores'] = $item['channel'];
                }
                $list['data'][$key]['pay_time'] = date('Y-m-d',$item['pay_time']);
                $list['data'][$key]['validity_time'] = date('Y-m-d',$item['validity_time']);

            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        if(!empty($_GET['uid'])){
            $_GET['name'] = db('user')->where('user_id',$_GET['uid'])->value('name');
        }else{
            $_GET['name'] = '';
        }

        return view('list',['name'=>$_GET['name']]);
    }


    function edit(){
        $id = input('id');
        $list = Db::table('je_card_order')->alias('c') ->join('je_membership_card m','c.card_id = m.card_id')->join('je_user u','c.user_id = u.user_id')->field('u.name as uname,u.phone,m.type as stype,c.*')->order('card_order_id desc')->where("card_order_id={$id}")->find();

            if($list['channel']==1){
                $list['stores'] = Db::table('je_store_user') ->alias('su')->join('je_store s','su.store_id = s.store_id') ->field('su.name as suname,s.name as sname')->where("su.sn='{$list['store_user_sn']}'")->find();
            }elseif ($list['channel']==3){
                $list['stores'] = db('user')->where('user_id',$list['store_user_sn'])->value('name');
            }else{
                $list['stores'] = $list['channel'];
            }

            if($list['card_status']==1){
                $list['order_pay'] = db('order_pay')->where("order_id={$id} and type=3")->find();
            }
        return view('edit',['list'=>$list]);
    }

}